package com.cherry.json;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class TestJNI {
	
	static {
		
		System.loadLibrary("Project2");
	}
	
	public Father man = new Son();
	
	public String name = "jason";
	
	public static int num = 99;
	
	public native static String getStringFromC();
	
	public native String getStringFromCByInt(int num);
	
	public native String accessFieldFromC();
	
	public native int accessStaticFieldFromC();
	
	public native int CInkoveJava();
	
	public native String CInkoveStaticJava();
	
	public native void sortFromC(int[] array);
	
	public native int[] getIntArrayFromC(int len);
	
	/**
	 * 调用Date类生成时间戳
	 * @return
	 */
	public native Date getTimeFromC();
	
	public native void invokeSuperMethodFromC();
	
	public native String getChineseStrFromC(String originString);
	
	public native void exceptionFromC();
	
	public static void main(String[] args) {
		
		System.out.println(getStringFromC());
		
		TestJNI testJNI = new TestJNI();
		
		System.out.println(testJNI.getStringFromCByInt(88));
		
		System.out.println("修改前"+testJNI.name);
		
		System.out.println(testJNI.accessFieldFromC());
		
		System.out.println("修改后"+testJNI.name);
		
		System.out.println("修改前"+num);
		
		System.out.println(testJNI.accessStaticFieldFromC());
		
		System.out.println("修改后"+num);
		
		System.out.println(testJNI.CInkoveJava());	
		
		System.out.println(testJNI.CInkoveStaticJava());
		
		System.out.println(testJNI.getTimeFromC());
		
		testJNI.invokeSuperMethodFromC();
	
		System.out.println(testJNI.getChineseStrFromC("你好"));
		
		int[] array= {200,5,33,43,2,34};
		testJNI.sortFromC(array);
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		System.out.println("-------getIntArrayFromC--------");
		int[] newArray = testJNI.getIntArrayFromC(5);
		for (int i = 0; i < newArray.length; i++) {
			System.out.println(newArray[i]);
		}
		
		try {
			testJNI.exceptionFromC();
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public int random(int num) {
		System.out.println("我被C程序调用了");
		return new Random().nextInt(num);
	}
	
	public static String uuid() {
		System.out.println("我被C程序调用了---static");
		return UUID.randomUUID().toString();
	}
	
}
